#include"../include/set.h"

int insert(void *self, void *key) {  
    Set *set=(Set*)self;
    if (search(set->tree, key) != NULL) {  
        return 0; // 已存在，不添加  
    }  
    set->size++;
    RB_insert(set->tree, key, NULL);  
    return 1; // 添加成功  
}  
// 将 Set 转换为数组  
Array* data(void *self) {  
    Set *set=(Set*)self;
    return tree_to_array(set->tree); // 使用之前定义的 tree_to_array 函数  
} 
int contains(void *self, void *key) {  
    Set *set=(Set*)self;
    return search(set->tree, key) != NULL;  
}  
void erase(void *self, void *key) {  
    Set *set=(Set*)self;
    set->size--;
    delete_node(set->tree, key);  
}  
void clear(void *self) {  
    Set *set=(Set*)self;
    free(set->tree); // 这里应添加释放树节点的完整代码  
    free(set);  
}

Set* set(int (*compare)(const void *, const void *)) {  
    Set *set = malloc(sizeof(Set));  
    set->tree = malloc(sizeof(RBTree));  
    set->tree->root = NULL;  
    set->tree->compare = compare;  
    set->tree->size = 0;  
    set->size=0;
    set->clear=clear;
    set->contains=contains;
    set->data=data;
    set->erase=erase;
    set->insert=insert;
    return set;  
}  
